﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;
namespace GetPinYin
{
    /// <summary>
    /// 使用微软字库
    /// </summary>
    public static class PinYinHelperB
    {
        /// <summary>
        /// 获得一个字符串的拼音
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPinYin(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            foreach (char item in str)
            {
                //判断是不是汉字，如果不是原字符返回
                if (ChineseChar.IsValidChar(item))
                {
                    sb.Append(GetPinYin(item));
                }
                else
                {
                    sb.Append(item);
                }
            }


            return sb.ToString();
        }


        /// <summary>
        /// 获得单个字符的拼音
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetPinYin(char c)
        {
            ChineseChar cc = new ChineseChar(c);
            string str = cc.Pinyins[0]; //多音字只取第一个


            return str.Substring(0, str.Length - 1); //去掉最后的声调
        }
    }
}
